package com.easyidea.its.web.action.department;

import java.util.ArrayList;
import java.util.List;

import com.easyidea.its.domain.model.system.Department;
import com.easyidea.its.facade.department.DepartmentMgr;
import com.easyidea.its.util.ItsConstants;
import com.easyidea.its.util.PageUtil;
import com.easyidea.its.util.Result;
import com.easyidea.its.util.SessionUtil;
import com.easyidea.its.web.action.base.ItsBaseAction;

public class DepartmentAction extends ItsBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2251611987725048030L; 
	private DepartmentMgr departmentMgr;
	
	private Department dept;
	private Integer id;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@SuppressWarnings("unchecked")
	public String departmentList() {
		if(dept == null) {
			dept = new Department();
		}
		int totalRecords = departmentMgr.getDeparmentsCount(dept);
		if (totalRecords > 0) {
			List<Department> records = departmentMgr.getDepartments(dept, (currentPage - 1) * pageSize,
					pageSize);
			result = PageUtil.createResult(pageSize, currentPage, totalRecords, records);
		} else {
			result = PageUtil.createResult(pageSize, currentPage, 0, new ArrayList());
		}
		return SUCCESS;
	}
	
	public String departmentAdd() {
		
		if(dept == null) {
			dept = new Department();
		}
		return SUCCESS;
	}
	public String departmentEdit() {
		
		dept = departmentMgr.getDepartmentById(id);
		return SUCCESS;
	}
	public String  saveOrUpdate() {
		boolean ret = departmentMgr.saveOrUpdate(dept);
		if (!ret) {
			errorMsg = "部门名称存在重复！";
			return INPUT;
		}
		SessionUtil.getSession().setAttribute("successMsg", "save.success");
		return SUCCESS;
	}
	public String departmentDel() {
			
			try {
				departmentMgr.removeDepartment(id);
				getSession().setAttribute("successMsg", "delete.success");
			}catch(Exception e){
				getSession().setAttribute("successMsg", "delete.department.failure");
			}

		return SUCCESS;
	}
	public DepartmentMgr getDepartmentMgr() {
		return departmentMgr;
	}

	public void setDepartmentMgr(DepartmentMgr departmentMgr) {
		this.departmentMgr = departmentMgr;
	}
	private Result<Department> result;
	
	public Department getDept() {
		return dept;
	}
	public void setDept(Department dept) {
		this.dept = dept;
	}
	public Result<Department> getResult() {
		return result;
	}
	public void setResult(Result<Department> result) {
		this.result = result;
	}	
}
